<template>
  <div class="inventory-freeze-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>生产计划和排产</span>
          <el-button type="primary" @click="handleCreate">新建冻结单</el-button>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="冻结单号">
          <el-input v-model="queryForm.freezeNo" placeholder="请输入冻结单号" />
        </el-form-item>
        <el-form-item label="仓库">
          <el-select
            v-model="queryForm.warehouse"
            placeholder="请选择仓库"
            clearable
          >
            <el-option
              v-for="item in warehouseOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="操作类型">
          <el-select v-model="queryForm.type" placeholder="请选择操作类型">
            <el-option label="冻结" value="freeze" />
            <el-option label="解冻" value="unfreeze" />
          </el-select>
        </el-form-item>
        <el-form-item label="操作日期">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="queryForm.status" placeholder="请选择状态">
            <el-option label="待审核" value="pending" />
            <el-option label="已审核" value="approved" />
            <el-option label="已完成" value="completed" />
            <el-option label="已取消" value="cancelled" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="expand">
          <template #default="props">
            <el-table :data="props.row.items" border>
              <el-table-column prop="materialCode" label="物料编码" />
              <el-table-column prop="materialName" label="物料名称" />
              <el-table-column prop="specification" label="规格型号" />
              <el-table-column prop="unit" label="单位" />
              <el-table-column prop="warehouse" label="仓库" />
              <el-table-column prop="location" label="库位" />
              <el-table-column prop="batchNo" label="批次号" />
              <el-table-column prop="quantity" label="数量" />
              <el-table-column prop="reason" label="原因" />
            </el-table>
          </template>
        </el-table-column>
        <el-table-column prop="freezeNo" label="冻结单号" width="180" />
        <el-table-column prop="warehouse" label="仓库" width="120" />
        <el-table-column prop="type" label="操作类型" width="100">
          <template #default="scope">
            <el-tag :type="scope.row.type === 'freeze' ? 'danger' : 'success'">
              {{ scope.row.type === "freeze" ? "冻结" : "解冻" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="operateDate" label="操作日期" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="creator" label="创建人" width="120" />
        <el-table-column label="操作" width="250" fixed="right">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleEdit(scope.row)"
              v-if="scope.row.status === 'pending'"
              >编辑</el-button
            >
            <el-button
              link
              type="primary"
              @click="handleApprove(scope.row)"
              v-if="scope.row.status === 'pending'"
              >审核</el-button
            >
            <el-button
              link
              type="success"
              @click="handleExecute(scope.row)"
              v-if="scope.row.status === 'approved'"
              >执行</el-button
            >
            <el-button link type="primary" @click="handleView(scope.row)"
              >查看</el-button
            >
            <el-button
              link
              type="danger"
              @click="handleCancel(scope.row)"
              v-if="scope.row.status === 'pending'"
              >取消</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 新建/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'create' ? '新建冻结单' : '编辑冻结单'"
      width="80%"
    >
      <el-form :model="form" label-width="100px" :rules="rules" ref="formRef">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="仓库" prop="warehouse">
              <el-select
                v-model="form.warehouse"
                placeholder="请选择仓库"
                @change="handleWarehouseSelect"
              >
                <el-option
                  v-for="item in warehouseOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择操作类型">
                <el-option label="冻结" value="freeze" />
                <el-option label="解冻" value="unfreeze" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="操作日期" prop="operateDate">
              <el-date-picker
                v-model="form.operateDate"
                type="date"
                placeholder="请选择操作日期"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-table :data="form.items" border class="mt-20">
          <el-table-column type="index" width="50" />
          <el-table-column label="物料" width="280">
            <template #default="scope">
              <el-select
                v-model="scope.row.materialCode"
                filterable
                remote
                placeholder="请选择物料"
                :remote-method="handleMaterialSearch"
                :loading="materialLoading"
                @change="(val) => handleMaterialSelect(val, scope.$index)"
              >
                <el-option
                  v-for="item in materialOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="materialName" label="物料名称" />
          <el-table-column prop="specification" label="规格型号" />
          <el-table-column prop="unit" label="单位" width="80" />
          <el-table-column label="库位" width="200">
            <template #default="scope">
              <el-select
                v-model="scope.row.location"
                placeholder="请选择库位"
                :disabled="!scope.row.materialCode"
              >
                <el-option
                  v-for="item in locationOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="批次" width="150">
            <template #default="scope">
              <el-select
                v-model="scope.row.batchNo"
                placeholder="请选择批次"
                :disabled="!scope.row.location"
              >
                <el-option
                  v-for="item in batchOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="数量" width="150">
            <template #default="scope">
              <el-input-number
                v-model="scope.row.quantity"
                :min="0"
                :precision="2"
                :disabled="!scope.row.batchNo"
              />
            </template>
          </el-table-column>
          <el-table-column label="原因" width="200">
            <template #default="scope">
              <el-input v-model="scope.row.reason" placeholder="请输入原因" />
            </template>
          </el-table-column>
          <el-table-column label="操作" width="80">
            <template #default="scope">
              <el-button
                link
                type="danger"
                @click="handleRemoveItem(scope.$index)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>

        <div class="table-operations">
          <el-button type="primary" @click="handleAddItem">添加物料</el-button>
        </div>

        <el-form-item label="备注">
          <el-input
            v-model="form.remark"
            type="textarea"
            :rows="3"
            placeholder="请输入备注信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
// ... 省略状态定义和方法实现 ...
</script>

<style scoped>
.inventory-freeze-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.table-operations {
  margin: 20px 0;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.mt-20 {
  margin-top: 20px;
}

.dialog-footer {
  text-align: right;
}
</style>
